package com.clear.honghefinance.biz.modules.assist_calculate_summary.handler.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.dto.*;
import com.clear.honghefinance.biz.modules.system.member.domain.vo.AdminDTO;
import com.clear.honghefinance.biz.modules.assist_calculate.enums.AssistCalculateCategoryCodeEnum;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.converter.AssistCalculateCustomConvertor;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.entity.AssistCalculateCustom;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.vo.GetAssistCalculateCustomVO;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.vo.ListAssistCalculateBaseVO;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.handler.AssistCalculateHandler;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.service.IAssistCalculateCustomService;
import com.clear.honghefinance.common.exception.BizException;
import com.clear.honghefinance.common.service.TokenService;
import com.clear.honghefinance.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class AssistCalculateCustomHandler implements AssistCalculateHandler {

    @Resource
    private IAssistCalculateCustomService assistCalculateCustomService;
    @Resource
    private TokenService<AdminDTO> tokenService;

    /**
     * 枚举身份的识别
     *
     * @return
     */
    @Override
    public AssistCalculateCategoryCodeEnum getHandlerType() {
        return AssistCalculateCategoryCodeEnum.CUSTOM;
    }

    /**
     * 创建辅助核算
     *
     * @param dto
     */
    @Override
    public boolean create(Object dto) {
        AssistCalculateCustom assistCalculate = AssistCalculateCustomConvertor.INSTANCE.convertDTOToEntity((CreateAssistCalculateCustomDTO) dto);
        assistCalculate.initDefault();
        assistCalculate.setTenantId(tokenService.getThreadLocalTenantId());
        assistCalculate.setMemberId(tokenService.getThreadLocalUserId());
        assistCalculate.setUpdateMemberId(tokenService.getThreadLocalUserId());

        if (assistCalculateCustomService.save(assistCalculate)) {
            return true;
        }
        throw new BizException("创建失败");
    }

    /**
     * 修改辅助核算
     *
     * @param dto
     * @return
     */
    @Override
    public boolean update(Object dto) {
        AssistCalculateCustom assistCalculate = AssistCalculateCustomConvertor.INSTANCE.convertDTOToEntity((UpdateAssistCalculateCustomDTO) dto);
        assistCalculate.setUpdateMemberId(tokenService.getThreadLocalUserId());
        assistCalculate.setUpdateTime(DateUtil.getSystemTime());
        LambdaUpdateWrapper<AssistCalculateCustom> lambdaUpdateWrapper = Wrappers.<AssistCalculateCustom>lambdaUpdate()
                .eq(AssistCalculateCustom::getId, assistCalculate.getId())
                .eq(AssistCalculateCustom::getTenantId, assistCalculate.getTenantId());

        if (assistCalculateCustomService.update(assistCalculate, lambdaUpdateWrapper)) {
            return true;
        }
        throw new BizException("创建失败");
    }

    /**
     * 根据辅助核算id查询辅助核算列表
     *
     * @param ids 辅助核算id列表
     * @return
     */
    @Override
    public List<? extends ListAssistCalculateBaseVO> listByAssistCalculateSummaryIds(List<Long> ids) {
        try {
            LambdaQueryWrapper<AssistCalculateCustom> lambdaQueryWrapper = Wrappers.<AssistCalculateCustom>lambdaQuery()
                    .eq(AssistCalculateCustom::getTenantId, tokenService.getThreadLocalTenantId())
                    .eq(AssistCalculateCustom::getDelFlag, false)
                    .in(CollectionUtils.isNotEmpty(ids), AssistCalculateCustom::getAssistCalculateSummaryId, ids)
                    .select(AssistCalculateCustom::getId,
                            AssistCalculateCustom::getDisable,
                            AssistCalculateCustom::getAssistCalculateSummaryId,
                            AssistCalculateCustom::getC1,
                            AssistCalculateCustom::getC2,
                            AssistCalculateCustom::getC3,
                            AssistCalculateCustom::getC4,
                            AssistCalculateCustom::getC5,
                            AssistCalculateCustom::getC6,
                            AssistCalculateCustom::getC7,
                            AssistCalculateCustom::getC8,
                            AssistCalculateCustom::getC9,
                            AssistCalculateCustom::getC10
                    );
            List<AssistCalculateCustom> list = assistCalculateCustomService.list(lambdaQueryWrapper);

            return AssistCalculateCustomConvertor.INSTANCE.convertEntityToVO(list);
        } catch (Exception e) {
            throw new BizException("查询辅助自定义核算列表失败");
        }
    }

    /**
     * 根据辅助核算id查询辅助核算详情
     *
     * @param id 辅助核算id
     * @return
     */
    @Override
    public GetAssistCalculateCustomVO getDetailById(long id) {
        try {
            LambdaQueryWrapper<AssistCalculateCustom> lambdaQueryWrapper = Wrappers.<AssistCalculateCustom>lambdaQuery()
                    .eq(AssistCalculateCustom::getTenantId, tokenService.getThreadLocalTenantId())
                    .eq(AssistCalculateCustom::getDelFlag, false)
                    .eq(AssistCalculateCustom::getAssistCalculateSummaryId, id)
                    .select(AssistCalculateCustom::getC1,
                            AssistCalculateCustom::getC2,
                            AssistCalculateCustom::getC3,
                            AssistCalculateCustom::getC4,
                            AssistCalculateCustom::getC5,
                            AssistCalculateCustom::getC6,
                            AssistCalculateCustom::getC7,
                            AssistCalculateCustom::getC8,
                            AssistCalculateCustom::getC9,
                            AssistCalculateCustom::getC10
                    );
            AssistCalculateCustom assistCalculate = assistCalculateCustomService.getOne(lambdaQueryWrapper);

            return AssistCalculateCustomConvertor.INSTANCE.convertEntityToVO(assistCalculate);
        } catch (Exception e) {
            throw new BizException("查询辅助自定义核算详情失败");
        }
    }


}
